<template>
	<view class="content">
		<navbar isBack :title="navTile">
		</navbar>
		<view class="u-page pl20 pr20">
			<u-form :label-style="labelStyle" :model="form" ref="uForm" label-position="top">
				<u-form-item label="用户姓名" prop="name">
					<input :disabled="pageType == 'detail'" :adjust-position="true" :cursor-spacing="100"
						placeholder="请输入用户姓名" v-model="form.name" />
				</u-form-item>
				<u-form-item label="用户手机号" prop="phone">
					<input disabled :adjust-position="true" :cursor-spacing="100" placeholder="用户手机号"
						v-model="form.phone" />
				</u-form-item>
				<u-form-item label="创建时间" prop="time">
					<input disabled :adjust-position="true" :cursor-spacing="100" v-model="form.time"
						placeholder="用户创建时间" />
				</u-form-item>
				<view style="position: relative;">
					<u-form-item label="分配角色" prop="role">
						<input disabled :adjust-position="true" :cursor-spacing="100" placeholder="请选择角色"
							v-model="form.role" @click="handShowRole" />
					</u-form-item>
					<view v-if="pageType=='edit'" style="color: #fff;
						position: absolute;
						width: 200rpx;
						height: 60rpx;
						background: #ff0000;
						border-radius: 10rpx;
						text-align: center;
						line-height: 60rpx;
						bottom: 20rpx;
						right: 20rpx;
						z-index: 10;" @click.stop="clearRole">
						清空选择
					</view>
				</view>
			</u-form>
		</view>
		<u-select safe-area-inset-bottom title="选择角色" v-model="showRole" :list="roleList" :default-value="defaultValue"
			@confirm="confirm"></u-select>
		<footerBottom v-if="pageType!='detail'">
			<view class="pl30 pr30 z-flex z-flex-align-c" style="height: 100%;">
				<view style="width: 100%;">
					<u-button :loading="btnLoading" type="primary" :custom-style="customStyle" shape="circle"
						@click="add">
						编辑
					</u-button>
				</view>
			</view>
		</footerBottom>
	</view>
</template>

<script>
	import {
		editUserApi,
		userDetailApi,
		roleSelectApi
	} from './api/index.js'
	export default {
		data() {
			return {
				pageType: 'detail',
				navTile: '用户详情',
				btnLoading: false,
				labelStyle: {
					fontSize: '30rpx'
				},
				customStyle: {
					background: '#1abc9c'
				},
				form: {
					name: '',
					remark: ''
				},
				rules: {

				},
				showRole: false,
				roleList: [],
				defaultValue: [0],
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		watch: {
			showRole(val) {
				if (val) {
					let index = this.roleList.findIndex(i => i.value == this.form.roleId)
					this.defaultValue = [index == -1 ? 0 : index]
				} else {
					this.defaultValue = [0]
				}
			}
		},
		onLoad(p) {
			if (p.pageType == 'edit') {
				this.pageType = p.pageType
				this.navTile = p.navTile
			}
			this.userDetail(p._id)
			this.getroleList()
		},
		methods: {
			handShowRole() {
				if (this.pageType == 'edit') {
					this.showRole = true
				}
			},
			clearRole() {
				this.form.role = ''
				this.form.roleId = ''
			},
			confirm(val) {
				this.form.role = val[0].label
				this.form.roleId = val[0].value
			},
			add() {
				this.btnLoading = true
				this.$refs.uForm.validate(async (valid) => {
					if (valid) {
						try {
							let {
								code,
								data
							} = await editUserApi(this.form)
							if (code == 0) {
								let canNavBack = getCurrentPages()
								if (canNavBack && canNavBack.length > 1) {
									uni.navigateBack()
								} else {
									let path = 'pages/index/index'
									uni.$u.route({
										type: 'switchTab',
										url: path
									})
								}
							}
						} catch (e) {
							//TODO handle the exception
						}

					} else {}
					this.btnLoading = false
				});
			},
			async userDetail(_id) {
				const {
					data
				} = await userDetailApi({
					_id
				})
				this.form = data
			},
			async getroleList() {
				const {
					data
				} = await roleSelectApi()
				this.roleList = data.map(i => {
					return {
						value: i._id,
						label: i.name
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tedian {
		// border: 2rpx solid #68be84;
		border: 2rpx solid #eee;
		color: #666;
		padding: 5rpx 30rpx;
		border-radius: 5rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		margin-right: 10rpx;
		margin-bottom: 10rpx;

		&:last-child {
			margin-right: 0;
		}

		.check {
			position: absolute;
			right: 0;
			top: 0;
			height: 28rpx;
			width: 28rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-bottom-left-radius: 4rpx;
			background: #68be84;
			transition: all 0.5s;
		}
	}

	.seltedian {
		border: 2rpx solid #68be84 !important;
		color: #68be84 !important;
		transition: all 0.5s;
	}

	/deep/.u-form-item__message {
		margin-top: 0 !important;
		position: absolute;
		bottom: 0;
		font-size: 20rpx;
	}

	/deep/.u-select__header__confirm {
		font-size: 40rpx;
	}

	/deep/.u-select__header__cancel {
		font-size: 40rpx;
	}
</style>